﻿using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Habanero.BO;
using UseCaseManager.BO;

namespace UseCaseManager.Logic
{
    public static class CreateXml
    {
        //experiment: not tested, or used
        public static void WritingFile()
        {
            XmlAttributes attributes = new XmlAttributes();
            attributes.XmlElements.Add(new XmlElementAttribute("Drop", typeof (Drop)));
            attributes.XmlElements.Add(new XmlElementAttribute("Use Case", typeof (UseCase)));
            XmlAttributeOverrides attributeOverrides= new XmlAttributeOverrides();
           // attributeOverrides.Add(typeof(BusinessObject),"Business Object",attributes);
            attributeOverrides.Add(typeof(BusinessObjectCollection<Drop>), "Drop", attributes);
            attributeOverrides.Add(typeof(BusinessObjectCollection<UseCase>), "UseCase",attributes);
            BusinessObjectCollection<Drop> sampleCollection = Broker.GetBusinessObjectCollection<Drop>("", "");
            BusinessObjectCollection<UseCase> useCases = Broker.GetBusinessObjectCollection<UseCase>("", "");
            StreamWriter sw=new StreamWriter("BOs.xml");
            XmlSerializer xs = new XmlSerializer(typeof(BusinessObjectCollection<Drop>), attributeOverrides);
            XmlSerializer xsUseCase = new XmlSerializer(typeof(BusinessObjectCollection<UseCase>), attributeOverrides);
            xs.Serialize(sw,sampleCollection);
            xsUseCase.Serialize(sw, useCases);
            sw.Close();
        }

        public static void ProjectUseCases(Project project)
        {
            XmlWriterSettings settings=new XmlWriterSettings();
            //settings.Indent = true;
            settings.NewLineOnAttributes = true;

            XmlWriter writer = XmlWriter.Create("projectUseCases.xml", settings);
            writer.WriteStartDocument();

            //start writing elements and attributes
            writer.WriteStartElement("Project");
            writer.WriteAttributeString("Name",project.ProjectName);
            foreach (Package package in project.Packages)
            {
                writer.WriteStartElement("Package");
                writer.WriteAttributeString("Name",package.PackageName);
                foreach (UseCase useCase in package.UseCases)
                {
                    writer.WriteStartElement("UseCase");
                    writer.WriteAttributeString("Number",useCase.UseCaseNumber.ToString());
                    writer.WriteAttributeString("Name",useCase.UseCaseName);
                    writer.WriteAttributeString("EventTrigger",useCase.Trigger);
                    writer.WriteStartElement("Response");
                    foreach (UseCaseStep useCaseStep in useCase.UseCaseSteps)
                    {
                        writer.WriteElementString("UseCaseStep",useCaseStep.StepDescription); 
                        writer.WriteEndElement();
                    }
                   writer.WriteEndElement();
                  
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            
            writer.WriteEndDocument();
            //clean up
            writer.Flush();
            writer.Close();
        }
    }
}
